#!/bin/bash

# 编译器配置
HOST_ARCH=$(uname -sm)
TARGET_PLATFORMS=(
    # x86_64平台
    "windows x86_64 x86_64-w64-mingw32"
    "linux x86_64 x86_64-linux-gnu"
    "darwin x86_64 x86_64-apple-darwin"
    
    # ARM平台
    "linux aarch64 aarch64-linux-gnu"
    "linux armv7 arm-linux-gnueabihf"
    "darwin arm64 arm64-apple-darwin"
)

# 清理旧构建
rm -rf build
rm -f db转sql工具.zip
mkdir -p build

# 检查依赖
if [ ! -f "sqlite3.c" ] || [ ! -f "miniz.c" ] || [ ! -f "$1" ]; then
    echo "缺少必要的源文件 (需要 sqlite3.c, miniz.c 和 C++ 源文件)"
    exit 1
fi

# 编译公共对象文件
compile_common() {
    echo "编译公共对象文件..."
    
    # 编译 SQLite
    $CC -c sqlite3.c -o sqlite3.o $CFLAGS_COMMON || exit 1
    
    # 编译 miniz
    $CC -c miniz.c -o miniz.o $CFLAGS_COMMON || exit 1
}

# 主编译函数
build_target() {
    local platform=$1
    local arch=$2
    local triple=$3
    
    echo -e "\n正在编译 $platform ($arch)"
    mkdir -p build/$platform-$arch

    # 设置系统显示名称
    case $platform in
        "windows") system_name="windows" ;;
        "linux")   system_name="linux" ;;
        "darwin")  system_name="macos" ;;
    esac

    # 设置工具链和编译选项
    case "$platform-$arch" in
        "windows-x86_64")
            CC=x86_64-w64-mingw32-gcc
            CXX=x86_64-w64-mingw32-g++
            EXT=.exe
            FLAGS="-static-libgcc -static-libstdc++"
            ;;
        "linux-x86_64")
            CC=gcc
            CXX=g++
            EXT=""
            FLAGS=""
            ;;
        "linux-aarch64")
            CC=aarch64-linux-gnu-gcc
            CXX=aarch64-linux-gnu-g++
            EXT=""
            FLAGS=""
            ;;
        "linux-armv7")
            CC=arm-linux-gnueabihf-gcc
            CXX=arm-linux-gnueabihf-g++
            EXT=""
            FLAGS="-march=armv7-a -mfpu=neon-vfpv4"
            ;;
        "darwin-x86_64")
            if [[ $HOST_ARCH != *"Darwin"* ]]; then
                echo "跳过 macOS 编译（需要在 macOS 主机执行）"
                return
            fi
            CC=clang
            CXX=clang++
            EXT=""
            FLAGS="-target x86_64-apple-macos10.13"
            ;;
        "darwin-arm64")
            if [[ $HOST_ARCH != *"Darwin"* ]]; then
                echo "跳过 macOS 编译（需要在 macOS 主机执行）"
                return
            fi
            CC=clang
            CXX=clang++
            EXT=""
            FLAGS="-target arm64-apple-macos11"
            ;;
        *)
            echo "未知平台架构组合: $platform-$arch"
            return
            ;;
    esac

    # 公共编译选项
    CFLAGS="$FLAGS -Os -I."
    LDFLAGS="$FLAGS -lpthread -lstdc++"

    # 编译公共对象文件
    CFLAGS_COMMON="$CFLAGS"
    compile_common

    # 生成标准化文件名
    output_file="sqlz-${system_name}-${arch}${EXT}"
    
    # 编译主程序
    echo "生成文件: $output_file"
    $CXX -o "build/$platform-$arch/$output_file" $1 sqlite3.o miniz.o $CFLAGS $LDFLAGS || exit 1

    # 清理中间文件
    rm -f sqlite3.o miniz.o
}

# 遍历所有目标平台
for target in "${TARGET_PLATFORMS[@]}"; do
    read -r platform arch triple <<< "$target"
    build_target "$platform" "$arch" "$triple"
done

# 打包所有平台
echo -e "\n打包发布文件..."
find build -type f -name 'sqlz-*' -exec cp -v {} build/ \;

zip -j db转sql工具.zip build/sqlz-*
echo "打包完成：db转sql工具.zip"

# 清理中间文件
rm -f build/*/sqlz-*